#!/bin/bash

BIN_PATH="/usr/local/bin"
CONFIG_PATH="/usr/local/etc/jbmon"
OPTS="monitor"
DAEMON="/sbin/start-stop-daemon"

if [ ! -x $BIN_PATH/jbmon ]; then
	echo "server was not been install"
	exit 0
fi

if [ ! -f $CONFIG_PATH/server.conf ]; then
	echo "server.conf was not found"
	exit 0
fi

. /lib/lsb/init-functions

case "$1" in
	start)
        log_daemon_msg "Starting jbmon monitor"
	if $DAEMON --start --quiet --oknodo -b -m --pidfile /var/run/jbmon.pid --exec $BIN_PATH/jbmon $OPTS; then
              log_end_msg 0
         else
              log_end_msg 1
         fi
	;;

	stop)
        log_daemon_msg "Stopping jbmon monitor"
        if $DAEMON  --stop --quiet --oknodo --pidfile /var/run/jbmon.pid; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
	
	restart)
	$DAEMON  --stop --quiet --oknodo --retry 5 --pidfile /var/run/jbmon.pid
	if $DAEMON  --start --quiet --oknodo -b -m --pidfile /var/run/jbmon.pid --exec $BIN_PATH/jbmon $OPTS; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
 status)
        status_of_proc -p /var/run/jbmon.pid $BIN_PATH/jbmon jbmon && exit 0 || exit $?
        ;;

  *)
        log_action_msg "Usage: /etc/init.d/jbmon {start|stop|relstart|status}"
        exit 1


esac
exit 0
